//
//  GLKTableView.m
//  KTools
//
//  Created by zhiyong.kuang on 2017/9/28.
//  Copyright © 2017年 zhiyong.kuang. All rights reserved.
//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                            O\ = /O
//                        ____/`---'\____
//                      .   ' \\| |// `.
//                       / \\||| : |||// \
//                     / _||||| -:- |||||- \
//                       | | \\\ - /// | |
//                     | \_| ''\---/'' | |
//                      \ .-\__ `-` ___/-. /
//                   ___`. .' /--.--\ `. . __
//                ."" '< `.___\_<|>_/___.' >'"".
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |
//                 \ \ `-. \_ __\ /__ _/ .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//
//         .............................................
//                  佛祖镇楼                  BUG辟易

#import "GLForm.h"
#import "GLFormRow.h"
#import "GLFormSection.h"


@interface GLForm()
@property(nonatomic,strong)NSMutableArray* sectioins;

@end

@implementation GLForm

#pragma mark- lifecycle
- (instancetype)init
{
    self = [super init];
    if (self) {
    }
    return self;
}

+(instancetype)formWithTag:(NSString*)tag{
    GLForm* form = [[GLForm alloc]initWithTag:tag];
    return form;
}
-(instancetype)initWithTag:(NSString*)tag{
    
    self = [super init];
    if (self) {
        _tag = tag;
    }
    return self;
}

-(void)addFormSection:(GLFormSection*)section{
    if (!section) {
        return;
    }
    [self.sectioins addObject:section];
}

- (void)removeFormSectionIndex:(NSInteger)index {
    if (index < self.sectioins.count) {
        [self.sectioins removeObjectAtIndex:index];
    }
}

#pragma mark-
-(NSMutableArray*)sectioins{
    if (!_sectioins) {
        _sectioins = [NSMutableArray arrayWithCapacity:0];
    }
    return _sectioins;
}


-(NSArray<GLFormRow *> *)formRowsWithTag:(NSString *)tag{
    NSMutableArray* array = [NSMutableArray arrayWithCapacity:0];
    for (GLFormSection* section in self.sectioins) {
        for (GLFormRow* row in section.rows) {
            if ([row.tag isEqualToString:tag]) {
                [array addObject:row];
            }
        }
    }
    
    return array;
}
-(GLFormRow *)formRowAtIndex:(NSIndexPath *)indexPath{
    if ((self.sectioins.count > indexPath.section) && [[self.sectioins objectAtIndex:indexPath.section] rows].count > indexPath.row){
        return [[[self.sectioins objectAtIndex:indexPath.section] rows] objectAtIndex:indexPath.row];
    }
    return nil;
}

-(NSIndexPath *)indexPathForFormRow:(GLFormRow *)formRow{
    
    for (NSInteger section = 0;section < self.sectioins.count;section++) {
        GLFormSection* formSection = [self.sectioins objectAtIndex:section];
        
        NSInteger row = [formSection.rows indexOfObject:formRow];
        if (row != NSNotFound) {
            return [NSIndexPath indexPathForRow:row inSection:section];
        }
    }
    return nil;
}

-(GLFormSection *)formSectionAtIndex:(NSUInteger)index{
    return [self.sectioins objectAtIndex:index];
}

-(NSInteger)indexformSection:(GLFormSection*)section{
    return [self.sectioins indexOfObject:section];
}

#pragma mark- values about
-(GLFormError*)validateFormValues{
    GLFormError* error = nil;
    for (GLFormSection* section in self.sectioins) {
        for (GLFormRow* row in section.rows) {
            if (row.isRequried && (!row.value || ([row.value isKindOfClass:[NSString class]] && [(NSString*)row.value isEqualToString:@""]))) {
                NSString* errmsg = row.requriedMsg;
                
                error = [GLFormError errorWithDomain:errmsg?errmsg:@"value can't been nil" code:GLErrorCodeValueNil userInfo:nil];
                error.tag = row.tag;
                return error;
            }
            
            if (!row.validateValueBlock){
                continue;
            }
            NSString* errMsg = row.validateValueBlock(row,row.value);
            if (errMsg) {
                error = [GLFormError errorWithDomain:errMsg code:GLErrorCodeValueValid userInfo:nil];
                error.tag = row.tag;
                return error;
            }
            
        }
    }
    return nil;
}

-(NSDictionary *)formValues{
    NSMutableDictionary* values = [NSMutableDictionary dictionaryWithCapacity:0];
    for (GLFormSection* section in self.sectioins) {
        for (GLFormRow* row in section.rows) {
            if (!row.tag || [row.tag isEqualToString:@""]) {
                continue;
            }
            if (!row.value) {
                continue;
            }
            [values setValue:row.value forKey:row.tag];
        }
    }
    return values;
}


-(void)dealloc{
    NSLog(@"");
}

@end


@implementation GLFormError
@end;

